home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / EDITORS / MEMACS / C / Archimedes next >
Text File  |  1990-07-11  |  11KB  |  596 lines

  1. /*
  2.  * The routines in this file provide support for the Archimedes and other
  3.  * compatible computers. It compiles into nothing if not an ARCHIMEDES driver
  4.  */
  5.  
  6. #define    termdef    1            /* don't define "term" external */
  7.  
  8. #include        <stdio.h>
  9. #include    "estruct.h"
  10. #include    "eproto.h"
  11. #include        "edef.h"
  12. #include    "elang.h"
  13.  
  14. #if     ACORN
  15.  
  16. #include    "kernel.h"
  17. #include    "swis.h"
  18.  
  19. #define NROW    64        /* max number of rows allowable */
  20. #define NCOL    144        /* max number of columns allowable */
  21. #define MARGIN    8        /* min margin for extended lines */
  22. #define SCRSIZ    64        /* size of scroll region for extended lines */
  23. #define NPAUSE    200        /* number of times through update to pause */
  24. #define BEL    0x07        /* BEL character */
  25. #define ESC    0x1B        /* ESC character */
  26. #define SPACE    32        /* space character */
  27.  
  28. /* Vector and event handling */
  29. #define EventOn        14
  30. #define EventOff    13
  31. #define KeyPress    0x0B
  32. #define EventV        0x10
  33. #define InsV        0x14
  34.  
  35. extern void Event_Handler(void);
  36. extern void Insert_Handler(void);
  37.  
  38. /* OS interface functions */
  39. static _kernel_swi_regs regs;
  40.  
  41. #define fx(a,x,y) (_kernel_osbyte(a,x,y))
  42. #define swi(n) (_kernel_swi(n,®s,®s))
  43. #define vdu(n) (void)(_kernel_oswrch(n))
  44.  
  45. #define vdu23(a,b,c,d,e,f,g,h,i) \
  46.     (void)( \
  47.     _kernel_oswrch(23), \
  48.     _kernel_oswrch(a), \
  49.     _kernel_oswrch(b), \
  50.     _kernel_oswrch(c), \
  51.     _kernel_oswrch(d), \
  52.     _kernel_oswrch(e), \
  53.     _kernel_oswrch(f), \
  54.     _kernel_oswrch(g), \
  55.     _kernel_oswrch(h), \
  56.     _kernel_oswrch(i)  \
  57.     )
  58.  
  59. #define vdu19(c,m,r,g,b) \
  60.     (void)( \
  61.     _kernel_oswrch(19), \
  62.     _kernel_oswrch(c), \
  63.     _kernel_oswrch(m), \
  64.     _kernel_oswrch(r), \
  65.     _kernel_oswrch(g), \
  66.     _kernel_oswrch(b)  \
  67.     )
  68.  
  69. int revflag = FALSE;
  70.  
  71. void armmove(int,int);
  72. void armeeol(void);
  73. void armputc(int);
  74. void armeeop(void);
  75. void armrev(int);
  76. int  armcres(char *);
  77. void armbeep(void);
  78. void armopen(void);
  79. void armclose(void);
  80. void armkopen(void);
  81. void armkclose(void);
  82. int  fnclabel(int, int);
  83.  
  84. void esc_off(void);
  85. void esc_on(void);
  86.  
  87. #define MDSENSE -1
  88. int scinit(int);
  89.  
  90. /* The current display mode, and associated VDU variables */
  91.  
  92. int dmode = -1;
  93.  
  94. static int mode_var_defs[] =
  95. {
  96.     0,        /* Mode flags */
  97.     1,        /* Maximum screen column */
  98.     2,        /* Maximum screen row */
  99.     3,        /* Maximum logical colour number */
  100.     -1
  101. };
  102.  
  103. #define NVARS ((sizeof(mode_var_defs) / sizeof(int)) - 1)
  104.  
  105. static struct {
  106.     int modeflags;
  107.     int maxcol;
  108.     int maxrow;
  109.     int maxcolour;
  110. }
  111. mode_var_vals;
  112.  
  113. /* Functions to save and restore the VDU state */
  114.  
  115. void save_vdustate(void);
  116. void restore_vdustate(void);
  117.  
  118. /* Saved values of the keyboard state */
  119.  
  120. static int esc_status;
  121. static int cursor_status;
  122. static int tab_status;
  123. #if 0
  124. static int kpad_status;
  125. static int kpad_shift;
  126. #endif
  127.  
  128. /* Vector handler private variables */
  129.  
  130. static struct {
  131.     int recursive;
  132.     int key;
  133.     int shift;
  134.     int ctrl;
  135.     int alt;
  136.     int international;
  137. }
  138. handler_vars;
  139.  
  140. static int fn_status[8];
  141.  
  142. #if    COLOR
  143. void armfcol(int);
  144. void armbcol(int);
  145.  
  146. int    ctran4[] =
  147. {
  148.     0,        /* Black */
  149.     1,        /* Red */
  150.     2,        /* Green == Yellow */
  151.     2,        /* Yellow */
  152.     1,        /* Blue == Red */
  153.     1,        /* Magenta == Red */
  154.     2,        /* Cyan == Yellow */
  155.     3        /* White */
  156. };
  157.  
  158. int    ctran255[] =
  159. {
  160.     0x00,        /* Black */
  161.     0x03,        /* Red */
  162.     0x0C,        /* Green */
  163.     0x0F,        /* Yellow */
  164.     0x30,        /* Blue */
  165.     0x33,        /* Magenta */
  166.     0x3C,        /* Cyan */
  167.     0x3F        /* White, Gray */
  168. };
  169. #endif
  170.  
  171. typedef int (PASCAL NEAR *termfn)();
  172.  
  173. /*
  174.  * Standard terminal interface dispatch table. Most of the fields point into
  175.  * "termio" code.
  176.  */
  177. TERM    term =
  178. {
  179.     NROW-1,         /* max number of rows allowable    */
  180.         NROW-1,             /* current number of rows used - set on init */
  181.         NCOL,            /* max number of columns */
  182.         NCOL,            /* current number of columns - set on init */
  183.     MARGIN,            /* min margin for extended lines */
  184.     SCRSIZ,            /* size of scroll region for extended lines */
  185.     NPAUSE,            /* number of times through update to pause */
  186.         (termfn)armopen,
  187.         (termfn)armclose,
  188.     (termfn)armkopen,
  189.     (termfn)armkclose,
  190.         (termfn)ttgetc,
  191.     (termfn)armputc,
  192.         (termfn)ttflush,
  193.         (termfn)armmove,
  194.         (termfn)armeeol,
  195.         (termfn)armeeop,
  196.         (termfn)armbeep,
  197.     (termfn)armrev,
  198.     (termfn)armcres
  199. #if    COLOR
  200.     ,
  201.     (termfn)armfcol,
  202.     (termfn)armbcol
  203. #endif
  204. };
  205.  
  206. #if    COLOR
  207. void armfcol (int color)    /* set the current output color */
  208. {
  209.     /* Not in teletext modes */
  210.     if ((mode_var_vals.modeflags & 0x02) != 0)
  211.         return;
  212.  
  213.     switch (mode_var_vals.maxcolour)
  214.     {
  215.     case 1:
  216.         break;
  217.  
  218.     case 3:
  219.         vdu(17);
  220.         vdu(ctran4[color & 0x07]);
  221.         break;
  222.  
  223.     case 15:
  224.         vdu(17);
  225.         vdu(color);
  226.         break;
  227.  
  228.     case 255:
  229.         vdu(17);
  230.         /* Special case for Grey == "Light Black" */
  231.         if (color == 8)
  232.             vdu(0x15);
  233.         else
  234.             vdu(ctran255[color & 0x07]);
  235.  
  236.         /* Set the tint based on the high bit */
  237.         if (color & 0x08)
  238.             vdu23(17,0,0xC0,0,0,0,0,0,0);
  239.         else
  240.             vdu23(17,0,0x00,0,0,0,0,0,0);
  241.  
  242.         break;
  243.     }
  244. }
  245.  
  246. void armbcol (int color)    /* set the current background color */
  247. {
  248.     /* Not in teletext modes */
  249.     if ((mode_var_vals.modeflags & 0x02) != 0)
  250.         return;
  251.  
  252.     switch (mode_var_vals.maxcolour)
  253.     {
  254.     case 1:
  255.         break;
  256.  
  257.     case 3:
  258.         vdu(17);
  259.         vdu(128+ctran4[color & 0x07]);
  260.         break;
  261.  
  262.     case 15:
  263.         vdu(17);
  264.         vdu(128+color);
  265.         break;
  266.  
  267.     case 255:
  268.         vdu(17);
  269.  
  270.         /* Special case for Grey == "Light Black" */
  271.         if (color == 8)
  272.             vdu(128+0x15);
  273.         else
  274.             vdu(128+ctran255[color & 0x07]);
  275.  
  276.         /* Set the tint based on the high bit */
  277.         if (color & 0x08)
  278.             vdu23(17,1,0xC0,0,0,0,0,0,0);
  279.         else
  280.             vdu23(17,1,0x00,0,0,0,0,0,0);
  281.  
  282.         break;
  283.     }
  284. }
  285. #endif
  286.  
  287. int spal (char *pstr)
  288. {
  289.     return(FALSE);
  290. }
  291.  
  292. void armmove (int row, int col)
  293. {
  294.     vdu(31);
  295.     vdu(col);
  296.     vdu(row);
  297. }
  298.  
  299. void armeeol (void)    /* erase to the end of the line */
  300. {
  301.     vdu23(8,5,6,0,0,0,0,0,0);
  302. }
  303.  
  304. void armputc (int ch)    /* put a character at the current position in the
  305.                current colors */
  306. {
  307.     vdu(ch);
  308. }
  309.  
  310. void armeeop (void)
  311. {
  312. #if    COLOR
  313.     armfcol(gfcolor);
  314.     armbcol(gbcolor);
  315. #endif
  316.     vdu23(8,4,10,0,0,0,0,0,0);
  317. }
  318.  
  319. void armrev (int state)    /* change reverse video state: TRUE = reverse, FALSE = normal */
  320. {
  321.     /* Only in non-teletext modes */
  322.     if ((mode_var_vals.modeflags & 0x02) == 0)
  323.     {
  324.         if (revflag != state)
  325.         {
  326.             revflag = state;
  327.             vdu23(17,5,0,0,0,0,0,0,0);
  328.         }
  329.     }
  330. }
  331.  
  332. int armcres (char *res)    /* change screen resolution */
  333. {
  334.     int mode = asc_int(res);
  335.     return(scinit(mode));
  336. }
  337.  
  338. void armbeep (void)
  339. {
  340.     vdu(BEL);
  341. }
  342.  
  343. void armopen (void)
  344. {
  345.     save_vdustate();
  346.     scinit(MDSENSE);
  347.     revflag = FALSE;
  348.  
  349.     /* Initialise the handler variables */
  350.     handler_vars.recursive = 0;
  351.     handler_vars.key   = 0;
  352.     handler_vars.shift = 0;
  353.     handler_vars.ctrl  = 0;
  354.     handler_vars.alt   = 0;
  355.  
  356.     /* Check for the International Keyboard module */
  357.     regs.r[0] = 18;
  358.     regs.r[1] = (int)"InternationalKeyboard";
  359.     handler_vars.international = (swi(OS_Module) ? 0 : 1);
  360.  
  361.     /* Now, open the keyboard */
  362.         ttopen();
  363.  
  364.     /* Switch the escape key off here, rather than in armkopen(),
  365.      * as we cannot afford to switch escape back on except under
  366.      * very controlled conditions.
  367.      */
  368.     esc_off();
  369. }
  370.  
  371. void armclose (void)
  372. {
  373.     restore_vdustate();
  374.  
  375.     /* Switch the escape key back on here */
  376.     esc_on();
  377. }
  378.  
  379. void esc_off (void)
  380. {
  381.     esc_status = fx(229,1,0) & 0xFF;
  382. }
  383.  
  384. void esc_on (void)
  385. {
  386.     fx(229,esc_status,0);
  387. }
  388.  
  389. static int init_mode;
  390.  
  391. static int init_var_defs[] =
  392. {
  393.     155,        /* Text foreground colour */
  394.     156,        /* Text background colour */
  395.     -1
  396. };
  397.  
  398. #define INIT_NVARS ((sizeof(init_var_defs) / sizeof(int)) - 1)
  399.  
  400. static struct {
  401.     int foreground;
  402.     int background;
  403. }
  404. init_var_vals;
  405.  
  406. void save_vdustate (void)
  407. {
  408.     init_mode = (fx(135,0,0) >> 8) & 0xFF;
  409.  
  410.     regs.r[0] = (int)init_var_defs;
  411.     regs.r[1] = (int)&init_var_vals;
  412.     swi(OS_ReadVduVariables);
  413. }
  414.  
  415. void restore_vdustate (void)
  416. {
  417.     if (init_mode != dmode)
  418.     {
  419.         vdu(22);
  420.         vdu(init_mode);
  421.     }
  422.  
  423.     vdu(17);
  424.     vdu(init_var_vals.foreground);
  425.     vdu(17);
  426.     vdu(128+init_var_vals.background);
  427. }
  428.  
  429. void armkopen (void)    /* open the keyboard */
  430. {
  431.     register int i;
  432.  
  433.     /* Cursor keys */
  434.     cursor_status = fx(4,2,0) & 0xFF;
  435.  
  436.     /* Tab key */
  437.     tab_status = fx(219,0x8A,0) & 0xFF;
  438.  
  439.     /* Function keys */
  440.     for (i = 0; i < 8; ++i)
  441.         fn_status[i] = fx(221+i,2,0) & 0xFF;
  442.  
  443. #if 0
  444.     /* Numeric keypad */
  445.     kpad_status = fx(238,0xC0,0) & 0xFF;
  446.     kpad_shift = fx(254,0,0) & 0xFF;
  447. #endif
  448.  
  449.     /* Start recording changes of shift key status */
  450.     fx(EventOn,KeyPress,0);
  451.  
  452.     regs.r[0] = EventV;
  453.     regs.r[1] = (int)Event_Handler;
  454.     regs.r[2] = (int)&handler_vars;
  455.     swi(OS_Claim);
  456.  
  457.     /* Switch our keyboard translation on */
  458.     regs.r[0] = InsV;
  459.     regs.r[1] = (int)Insert_Handler;
  460.     regs.r[2] = (int)&handler_vars;
  461.     swi(OS_Claim);
  462. }
  463.  
  464. void armkclose (void)    /* close the keyboard */
  465. {
  466.     register int i;
  467.  
  468.     /* Cursor keys */
  469.     fx(4,cursor_status,0);
  470.  
  471.     /* Tab key */
  472.     fx(219,tab_status,0);
  473.  
  474.     /* Function keys */
  475.     for (i = 0; i < 8; ++i)
  476.         fx(221+i,fn_status[i],0);
  477.  
  478. #if 0
  479.     /* Numeric keypad */
  480.     fx(238,kpad_status,0);
  481.     fx(254,kpad_shift,0);
  482. #endif
  483.     /* Finish recording changes of shift key status */
  484.     fx(EventOff,KeyPress,0);
  485.  
  486.     regs.r[0] = EventV;
  487.     regs.r[1] = (int)Event_Handler;
  488.     regs.r[2] = (int)&handler_vars;
  489.     swi(OS_Release);
  490.  
  491.     /* Switch our keyboard translation off */
  492.     regs.r[0] = InsV;
  493.     regs.r[1] = (int)Insert_Handler;
  494.     regs.r[2] = (int)&handler_vars;
  495.     swi(OS_Release);
  496. }
  497.  
  498. /* Set the screen up in a given mode. If mode is MDSENSE, then retain the
  499.  * current mode. Saves the various mode parameters, and resizes the current
  500.  * window as required.
  501.  */
  502.  
  503. int scinit (int mode)
  504. {
  505.     if (mode == MDSENSE)
  506.         mode = (fx(135,0,0) >> 8) & 0xFF;
  507.  
  508.     if (mode == dmode)
  509.         return(TRUE);
  510.  
  511.     /* Check that the requested mode is valid */
  512.     regs.r[0] = mode;
  513.     swi(OS_CheckModeValid);
  514.  
  515.     if (regs.r[0] != mode && regs.r[1] == -2)
  516.         return(FALSE);
  517.  
  518.     if (regs.r[0] != mode)
  519.         mode = regs.r[0];
  520.  
  521.     vdu(22);
  522.     vdu(mode);
  523.  
  524.     regs.r[0] = (int)mode_var_defs;
  525.     regs.r[1] = (int)&mode_var_vals;
  526.     swi(OS_ReadVduVariables);
  527.  
  528.     /* Correct the funny value for maxcol in 256-colour modes */
  529.     if (mode_var_vals.maxcolour == 63)
  530.         mode_var_vals.maxcolour = 255;
  531.  
  532.     /* Set the palette in 16-colour modes */
  533.     if (mode_var_vals.maxcolour == 15)
  534.     {
  535.         vdu19(0,16,0x00,0x00,0x00);    /* Black */
  536.         vdu19(1,16,0xCC,0x00,0x00);    /* Red */
  537.         vdu19(2,16,0x00,0xCC,0x00);    /* Green */
  538.         vdu19(3,16,0xCC,0xCC,0x00);    /* Yellow */
  539.         vdu19(4,16,0x00,0x00,0xCC);    /* Blue */
  540.         vdu19(5,16,0xCC,0x00,0xCC);    /* Magenta */
  541.         vdu19(6,16,0x00,0xCC,0xCC);    /* Cyan */
  542.         vdu19(7,16,0xCC,0xCC,0xCC);    /* Grey */
  543.         vdu19(8,16,0x77,0x77,0x77);    /* Gray == "Light Black" */
  544.         vdu19(9,16,0xFF,0x33,0x33);    /* Light Red */
  545.         vdu19(10,16,0x33,0xFF,0x33);    /* Light Green */
  546.         vdu19(11,16,0xFF,0xFF,0x33);    /* Light Yellow */
  547.         vdu19(12,16,0x33,0x33,0xFF);    /* Light Blue */
  548.         vdu19(13,16,0xFF,0x33,0xFF);    /* Light Magenta */
  549.         vdu19(14,16,0x33,0xFF,0xFF);    /* Light Cyan */
  550.         vdu19(15,16,0xFF,0xFF,0xFF);    /* White */
  551.     }
  552.  
  553.     newsize(TRUE,mode_var_vals.maxrow + 1);
  554.     newwidth(TRUE,mode_var_vals.maxcol + 1);
  555.  
  556.     /* reset the $sres environment variable */
  557.     sprintf(sres, "%d", mode);
  558.     dmode = mode;
  559.  
  560.     return(TRUE);
  561. }
  562.  
  563. /* write a line out */
  564. int scwrite (int row, char *outstr, int forg, int bacg)
  565. {
  566.     /* row    = row of screen to place outstr on */
  567.     /* outstr = string to write out (must be term.t_ncol long) */
  568.     /* forg   = forground color of string to write */
  569.     /* bacg   = background color */
  570.  
  571.     vdu(31);
  572.     vdu(0);
  573.     vdu(row);
  574.  
  575.     armfcol(forg);
  576.     armbcol(bacg);
  577.  
  578.     regs.r[0] = (int)outstr;
  579.     swi(OS_Write0);
  580.  
  581.     return 0;
  582. }
  583.  
  584. #if    FLABEL
  585. int fnclabel(int f, int n)    /* label a function key */
  586. {
  587.     /* on machines with no function keys...don't bother */
  588.     return(TRUE);
  589. }
  590. #endif
  591. #else
  592. armhello()
  593. {
  594. }
  595. #endif
  596.